package com.masterclient.app.businessmodule.homemodule.view.helper;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.SparseIntArray;
import android.view.View;

import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.masterclient.app.businessmodule.homemodule.view.IHomeMainView;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.SimpleMultiPurposeListener;

import static com.eoner.baselibrary.utils.ScreenUtils.dp2px;

public class HomeMainViewHelper {

    private IHomeMainView iHomeMainView;

    //recycle滑动相关变量
    private int distanceY;
    private float searchLayoutHeight = 0;
    private int alphaFloat;
    private int footerStickPosition = 0;

    public void setDistanceY(int distanceY) {
        this.distanceY = distanceY;
    }

    public void setFooterStickPosition(int footerStickPosition) {
        this.footerStickPosition = footerStickPosition;
    }

    public int getFooterStickPosition() {
        return footerStickPosition;
    }

    public int getAlphaFloat() {
        return alphaFloat;
    }

    public HomeMainViewHelper(int statusBarHeight, IHomeMainView iHomeMainView) {
        this.iHomeMainView = iHomeMainView;
        //变量
        this.distanceY = 0;
        this.searchLayoutHeight = dp2px(44) + statusBarHeight;
        this.alphaFloat = 0;
    }

    /**
    * 下拉刷新监听
    * */
    public void setRefreshLayoutListener(final SimpleMultiPurposeListener listener) {

       iHomeMainView.refreshLayout().setOnMultiPurposeListener(new SimpleMultiPurposeListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                distanceY = 0;
                if (listener != null) {
                    listener.onRefresh(refreshLayout);
                }
            }

            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                if (listener != null) {
                    listener.onLoadMore(refreshLayout);
                }
            }

            @Override
            public void onHeaderMoving(RefreshHeader header, boolean isDragging, float percent, int offset, int headerHeight, int maxDragHeight) {
            }
        });
    }


    public void setRecycleListener() {
        if (iHomeMainView != null) {
            iHomeMainView.recyclerView().addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);
                }

                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
                    distanceY += dy;
                    VirtualLayoutManager lm = (VirtualLayoutManager) recyclerView.getLayoutManager();
                    int first = 0, last = 0, total = 0;
                    first = ((LinearLayoutManager) lm).findFirstVisibleItemPosition();
                    last = ((LinearLayoutManager) lm).findLastVisibleItemPosition();
                    total = recyclerView.getAdapter().getItemCount();
                    //System.out.println(first + " ------- > " + last + " ------- > " + total);
                    int scrollY = 0;
                    SparseIntArray mRecyclerItemHeight = new SparseIntArray();
                    View c = recyclerView.getLayoutManager().getChildAt(0); //this is the first visible row
                    if (c != null) {
                        scrollY = -c.getTop();
                        mRecyclerItemHeight.put(first, c.getHeight());
                        for (int i = 0; i < first; ++i) {
                            scrollY += mRecyclerItemHeight.get(i); //add all heights of the views that are gone
                        }
                    }
                    float alpha = scrollY / searchLayoutHeight;
                    if (alpha < 0) {
                        alpha = 0;
                    }
                    if (alpha > 1) {
                        alpha = 1;
                    }
                    if (iHomeMainView != null) {
                        if (distanceY > 1000) {
                            iHomeMainView.backTopView().setVisibility(View.VISIBLE);
                        } else {
                            iHomeMainView.backTopView().setVisibility(View.GONE);
                        }
                        alphaFloat = (int) (alpha * 255);
                        if (alphaFloat > 255) {
                            alphaFloat = 255;
                        }
                        alphaFloat = 255 - alphaFloat;
                        if (first != 0) {
                            alphaFloat = 0;
                        }
                        iHomeMainView.refreshTopStateByAlpha(alphaFloat);
//                        Drawable drawable = iHomeMainView.mainToolBar().getBackground();
//                        if (drawable != null) {
//                            drawable.mutate().setAlpha(255 - alphaFloat);
//                        }

                        //吸顶白色
//                        if (first >= footerStickPosition) {
//                            iHomeMainView.setFooterTabState(1);
//                        } else {
//                            iHomeMainView.setFooterTabState(0);
//                        }
                    }
                }
            });
        }


    }

}
